import {
    Order
} from './order.model'
import {
    Member
} from './../member/member.model'
import Toast from '@vant/weapp/toast/toast'
const order = new Order()
const member = new Member()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        orderList: [],
        status: '999',
        page: 1,
        searchedTitle: '',
        currentId: '',
        currentPrice: '',
        refundModel: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this,
            status = options.status
        if (status != undefined) {
            that.setData({
                status: status
            })
        }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let that = this
        that.getTabBar().init() // 切换标签
        that._loadData()
    },

    /**
     * 获取所有数据
     */
    _loadData() {
        let that = this,
            status = parseInt(that.data.status),
            param = {
                status: status,
                page: 1
            }
        that.getOrderData(param)
    },

    /**
     * 获取订单
     * @param {*} param 要搜索的内容(订单号,检测项目名称,检车站)
     */
    getOrderData(param) {
        // 是否授权
        wx.getSetting({
            success: (res) => {
                if (res.authSetting['scope.userInfo']) {
                    wx.showLoading({
                        title: '正在加载数据',
                    })
                    let that = this
                    order.getOrderData(param).then((res) => {
                        that.setData({
                            orderList: res.data
                        })
                    }).then(() => {
                        wx.hideLoading()
                    })
                }
            },
            complete: (res) => {
                wx.stopPullDownRefresh()
                wx.hideNavigationBarLoading()
            }
        })
    },

    /**
     * 切换标签
     * @param {*} options
     */
    onSwitchStatus(options) {
        let that = this,
            status = parseInt(options.detail.name),
            searchedTitle = that.data.searchedTitle,
            param = {
                status: status,
                page: 1,
                searchedTitle: searchedTitle
            }
        that.setData({
            orderList: []
        })
        that.setData(param)
        that.getOrderData(param)
    },

    /**
     * 搜索车牌号
     * @param {*} options 搜索的值
     */
    onSearch(options) {
        let that = this,
            status = parseInt(that.data.status),
            param = {
                status: status,
                page: 1,
                searchedTitle: options.detail
            }
        that.setData({
            searchedTitle: options.detail
        })
        that.getOrderData(param)
    },

    /**
     * 支付
     * @param {*} options
     */
    doPay(options) {
        let order_id = order.getDataSet(options, 'order_id'),
            order_sn = order.getDataSet(options, 'order_sn'),
            total_amount = order.getDataSet(options, 'total_amount'),
            subscribe_id = order.getDataSet(options, 'subscribe_id'),
            orderData = {
                order_id: order_id,
                order_sn: order_sn,
                total_amount: total_amount,
                subscribe_id: subscribe_id
            }
        order.placeOrder(orderData).then((res) => {
            order.pay(res).then(() => {
                member.getMemberData().then((result) => {
                    let userInfo = order.getStorage('userInfo')
                    userInfo.member = result.data.member
                    order.setStorage('userInfo', userInfo)
                }).then(() => {
                    Toast({
                        type: 'success',
                        message: '支付成功',
                        onClose: () => {
                            wx.reLaunch({
                                url: '/pages/order/order?status=2&page=1'
                            })
                        },
                    })
                })
            }).catch(() => {
                Toast.fail('支付失败')
            })
        })
    },

    /**
     * 退款
     */
    onConfirmRefund() {
        let that = this
        order.refundOrder(that.data.currentId).then(() => {
            that.setData({
                refundModel: false
            })
        }).then(() => {
            wx.navigateTo({
                url: `/pages/refund/refund?price=${that.data.currentPrice}`,
            })
        })
    },

    /**
     * 打开退款窗口
     * @param {*} options
     */
    onOpenRefundModel(options) {
        let that = this,
            id = order.getDataSet(options, 'id'),
            price = order.getDataSet(options, 'price')
        that.setData({
            currentId: id,
            currentPrice: price,
            refundModel: true,
        });
    },

    /**
     * 关闭申请窗口
     */
    onCloseModel() {
        let that = this
        that.setData({
            refundModel: false
        });
    },

    /**
     * 跳转到评论列表
     * @param {*} options
     */
    onJumpCommentList() {
        wx.navigateTo({
            url: `/pages/comment/comment`,
        })
    },

    /**
     * 跳转到评论
     * @param {*} options
     */
    onJumpCommentWrite(options) {
        let id = order.getDataSet(options, 'id'),
            company_id = order.getDataSet(options, 'company_id')
        wx.navigateTo({
            url: `/pages/comment-write/comment-write?id=${id}&company_id=${company_id}`,
        })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        let that = this
        that.setData({
            page: 1,
            searchedTitle: ''
        })
        wx.showNavigationBarLoading()
        that._loadData()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        // 是否授权
        wx.getSetting({
            success: (res) => {
                if (res.authSetting['scope.userInfo']) {
                    wx.showLoading({
                        title: '正在加载数据',
                    })
                    let that = this,
                        searchedTitle = that.data.searchedTitle,
                        param = {
                            status: that.data.status,
                            page: that.data.page + 1,
                            searchedTitle: searchedTitle
                        }
                    that.setData({
                        page: that.data.page + 1,
                    })
                    order.getOrderData(param).then((res) => {
                        let result = that.data.orderList
                        result = result.concat(res.data)
                        that.setData({
                            orderList: result
                        })
                    }).then(() => {
                        wx.hideLoading()
                    })
                }
            }
        })
    }
})